package com.spark.common.upload.service;

import com.spark.common.upload.StorageService;
import com.spark.common.utils.FileUploadUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 本地存储实现
 *
 * @author LYCHEE
 * @date 2025/3/13 10:08
 */
@Configuration
@ConditionalOnProperty(name = "upload.type", havingValue = "local")
public class LocalStorageService implements StorageService {

    @Value("${upload.local.base-dir}")
    private String baseDir;

    @Value("${upload.local.access-path}")
    private String accessPath;


    @Override
    public String upload(MultipartFile file) throws IOException {
        String path = FileUploadUtils.upload(baseDir, file);
        return getFileUrl(path);
    }

    @Override
    public String getFileUrl(String fileName) {
        return accessPath.replace("/**", "/") + fileName;
    }
}
